package com.longteng.diamond.init;

import com.alibaba.fastjson.JSON;
import com.longteng.diamond.dao.QuartzMapper;
import com.longteng.diamond.domain.ScheduleJob;
import com.longteng.diamond.job.QuartzJob;
import com.longteng.diamond.service.QuartzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;


import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator
 * DATE : 2018/7/22
 * TIME : 22:47
 * PROJECT : java-lesson
 * PACKAGE : com.longteng.lesson
 *
 * @author <a href="mailto:taki@qq.com">Administrator</a>
 */
@Component
@Slf4j
public class InitQuartzJob {

    @Autowired
    private QuartzMapper quartzMapper;
    @Resource
    private QuartzService quartzService;
    @Bean(initMethod = "")
    public Object init() {
        log.info("开始初始化定时任务!!");
        List<ScheduleJob> scheduleJobList = quartzMapper.findQuartz(null);
        for (ScheduleJob scheduleJob : scheduleJobList) {
            List<String> weekTimeList = (List) JSON.parseArray(scheduleJob.getWeekTimeList());
            String jobTime = quartzService.getWeekTime(weekTimeList, scheduleJob.getHhTime(), scheduleJob.getMmTime());
            quartzService.addJob(scheduleJob, QuartzJob.class, jobTime);
        }
        log.info("初始化定时任务完毕!!");
        return null;
    }
}
